// pages/goodsList/goodsList.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgPath:app.globalData.imgPath,
    listmode:2,
    list:[],
    showzhBox: false,
    on:0,
    r:1,//1是搜索过来，2是分类过来,
    keyword:'',//搜索关键词，
    isorderSale: false,//是否按销量排序
    bypricedec: false,//价格排序倒序
    classList:[],//分类
    minf:'',//
    maxf:'',
    bysaledec:true,//销量倒序
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self=this;
    if(options.r==1){//搜索
      wx.setNavigationBarTitle({
        title: '商品搜索',
      })
      this.setData({ keyword: options.key,r:1 });
      wx.request({
        url: app.globalData.reqPath + 'shop/GoodsProducts',
        header: {
          'authorization': app.globalData.authorization
        },
        data: {
          productName: options.key,
          limit: -1,
        },
        success: function ({ data, header }) {
          if (data.code == 200) {
            if (header.authorization && header.authorization != app.globalData.authorization) {
              app.globalData.authorization = header.authorization;
            }
            if (data.data.length == 0) {
              self.setData({ empty: true })
            }
            self.setData({ list: data.data })
          }
        }
      })
    }else if(options.r==2){//分类
      this.setData({ classId: options.cid, r: 2 });
      wx.request({
        url: app.globalData.reqPath + 'shop/GoodsProducts',
        header: {
          'authorization': app.globalData.authorization
        },
        data: {
          categoryId: options.cid,
          limit: -1,
        },
        success: function ({ data, header }) {
          if (data.code == 200) {
            if (header.authorization && header.authorization != app.globalData.authorization) {
              app.globalData.authorization = header.authorization;
            }
            if (data.data.length == 0) {
              self.setData({ empty: true })
            }
            self.setData({ list: data.data })
          }
        }
      })
    }
    wx.request({//获取分类
      url: app.globalData.reqPath + 'shop/GoodsCategories',
      data: {
        limit: -1
      },
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data, header }) {
        if (header.authorization && header.authorization != app.globalData.authorization) {
          app.globalData.authorization = header.authorization;
        }
        let list = data.data;
        self.setData({ classList: list })
      }
    })
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  swichList:function(){
    let mode = this.data.listmode;
    if (mode == 1) {
      this.setData({ listmode: 2 })
    } else {
      this.setData({ listmode: 1 })
    }
  },
  showzhbox:function(){
    if (this.data.showzhBox) {
      this.setData({ showzhBox: false })
    } else {
      this.setData({ showzhBox: true })
    }
  },
  changezhorder:function(e){
    let on = e.currentTarget.dataset.on
    this.setData({ on: on, isorderSale: false });
    // console.log(on)
    let data = null;
    if (on == 0) {
      console.log(this.data.r)
      if (this.data.r == 1) {
        data = {
          productName: this.data.keyword,
          limit: -1,
          orderStr: 'addedDate.asc'
        }
        console.log(data)
      }else{
        data={
          categoryId: this.data.classId,
          limit:-1,
          orderStr: 'addedDate.asc'
        }
      }

    } else if (on == 1) {
      if (this.data.r == 1) {
        console.log(on)
        data = {
          productName: this.data.keyword,
          limit: -1,
          // offset: this.data.page * 10,
          orderStr: 'addedDate.desc'
        }
      }else{
        data = {
          categoryId: this.data.classId,
          limit: -1,
          orderStr: 'addedDate.desc'
        }
      }
    }

    this.getList(data)
  },
  getList: function (data) {
    let self = this;
    wx.request({
      url: app.globalData.reqPath + 'shop/GoodsProducts',
      header: {
        'authorization': app.globalData.authorization
      },
      data: data,
      success: function ({ data, header }) {
        if (data.code == 200) {
          if (header.authorization && header.authorization != app.globalData.authorization) {
            app.globalData.authorization = header.authorization;
          }
          self.setData({ list: data.data })
        }
      }
    })
  },
  changelistbysale:function(){//销量排序
    this.setData({ isorderSale: true })
    let self = this;
    if (this.data.r == 1) {
      if (this.data.bysaledec){
        wx.request({
          url: app.globalData.reqPath + 'shop/GoodsProducts',
          header: {
            'authorization': app.globalData.authorization
          },
          data: {
            productName: this.data.keyword,
            limit: -1,
            orderStr: 'saleCounts.asc'
          },
          success: function ({ data }) {
            self.setData({ list: data.data, bysaledec: false })
          }
        })
      }else{
        wx.request({
          url: app.globalData.reqPath + 'shop/GoodsProducts',
          header: {
            'authorization': app.globalData.authorization
          },
          data: {
            productName: this.data.keyword,
            limit: -1,
            orderStr: 'saleCounts.desc'
          },
          success: function ({ data }) {
            self.setData({ list: data.data, bysaledec: true })
          }
        })
      }        
    } else {
     // if (this.data.bysaledec){
          wx.request({
            url: app.globalData.reqPath + 'shop/GoodsProducts',
            header: {
              'authorization': app.globalData.authorization
            },
            data: {
              categoryId: this.data.classId,
              limit: -1,
              orderStr: 'saleCounts.asc'
            },
            success: function ({ data }) {
              self.setData({ list: data.data, bysaledec: false })
            }
          })
        // }else{
        //   wx.request({
        //     url: app.globalData.reqPath + 'shop/GoodsProducts',
        //     header: {
        //       'authorization': app.globalData.authorization
        //     },
        //     data: {
        //       categoryId: this.data.classId,
        //       limit: -1,
        //       orderStr: 'saleCounts.desc'
        //     },
        //     success: function ({ data }) {
        //       self.setData({ list: data.data,bysaledec:true})
        //     }
        //   })
        // }      
     }
  },
  orderbyprice:function(){//价格排序
    let desc = !this.data.bypricedec;
    this.setData({ isorderSale: false })
    let self = this
    if (desc) {
      if (this.data.r == 1) {
        wx.request({
          url: app.globalData.reqPath + 'shop/GoodsProducts',
          header: {
            'authorization': app.globalData.authorization
          },
          data: {
            productName: this.data.keyword,
            limit: -1,
            orderStr: 'salePrice.desc'
          },
          success: function ({ data }) {
            self.setData({ list: data.data })
          }
        })

     
      } else {
        wx.request({
          url: app.globalData.reqPath + 'shop/GoodsProducts',
          header: {
            'authorization': app.globalData.authorization
          },
          data: {
            categoryId: this.data.classId,
            limit: -1,
            orderStr: 'salePrice.desc'
          },
          success: function ({ data }) {
            self.setData({ list: data.data })
          }
        })
      }

    } else {
      if (this.data.r == 1) {
        wx.request({
          url: app.globalData.reqPath + 'shop/GoodsProducts',
          header: {
            'authorization': app.globalData.authorization
          },
          data: {
            productName: this.data.keyword,
            limit: -1,
            orderStr: 'salePrice.asc'
          },
          success: function ({ data }) {
            self.setData({ list: data.data })
          }
        })

      
      } else {
        wx.request({
          url: app.globalData.reqPath + 'shop/GoodsProducts',
          header: {
            'authorization': app.globalData.authorization
          },
          data: {
            categoryId: this.data.classId,
            limit: -1,
            orderStr: 'salePrice.asc'
          },
          success: function ({ data }) {
            self.setData({ list: data.data })
          }
        })
      }

    }
    this.setData({ bypricedec: desc });
  },
  filter: function () {
    let self = this;
  
    this.setData({ showfilterBox: true })
  },
  tapM: function (e) {
    this.setData({ showfilterBox: false });
  },
  selecclassfiy:function(e){
    let index = e.currentTarget.dataset.index;
    let list = this.data.classList;
    for (var i in list) {
      if (i == index) {
        list[i].selected = true;
      } else {
        list[i].selected = false;
      }
    }
    this.setData({ classList: list })
  },
  resetLimit:function(){
    let list1 = this.data.classList;
    list1 = list1.map(function (item) {
      item.selected = false;
      return item;
    })
    let data;
    if(this.data.r==1){//搜索
      data={
        productName:this.data.keyword,
        limit:-1
      }
    }else{//分类
      data = {
        categoryId: this.data.classId,
        limit: -1
      }
    }
    this.getList(data);
    this.setData({ minf: '', maxf: '',  classList: list1 });
  },
  confirmlimit:function(){//筛选框确定筛选
    let listclass = this.data.classList;
    let categoryId = [];
    for (let j in listclass) {
      if (listclass[j].selected) {
        categoryId.push(listclass[j].id);
      }
    }
    if (categoryId.length == 0 && this.data.minf == '' && this.data.maxf == "") {
      this.setData({ showfilterBox: false });
      return false;
    }
    let self = this;
    let data = null;
    if(this.data.r==1){//关键字搜索
      if (categoryId.length > 0) {//选定筛选框里的的分类
        if (this.data.maxf == "" && this.data.minf == "") {
          data = {
            categoryId: categoryId[0],
            productName: this.data.keyword,
          }
        } else
        if (this.data.minf == "") {
          data = {
            categoryId: categoryId[0],
            productName: this.data.keyword,
            // 'salePrice[0]': this.data.minf,
            'salePrice[1]': this.data.maxf
          }
        } else if (this.data.maxf == "") {
          data = {
            categoryId: categoryId[0],
            productName: this.data.keyword,
            'salePrice[0]': this.data.minf,
            // 'salePrice[1]': this.data.maxf
          }
        } else {
          data = {
            categoryId: categoryId[0],
            productName: this.data.keyword,
            'salePrice[0]': this.data.minf,
            'salePrice[1]': this.data.maxf
          }
        }



      } 

      


      }else{//分类
        if (categoryId.length > 0) {//选定筛选框里的的分类
          
          if (this.data.maxf == "" && this.data.minf == "") {//没填价格
            data = {
              categoryId: categoryId[0]
            }
          } else
            if (this.data.minf == ""&& this.data.maxf!="") {//
              data = {
                categoryId: categoryId[0],
                // 'salePrice[0]': this.data.minf,
                'salePrice[1]': this.data.maxf
              }
            } else if (this.data.maxf == "" && this.data.minf!= "") {
              data = {
                categoryId: categoryId[0],
                'salePrice[0]': this.data.minf,
                // 'salePrice[1]': this.data.maxf
              }
            } else {
              data = {
                categoryId: categoryId[0],
                'salePrice[0]': this.data.minf,
                'salePrice[1]': this.data.maxf
              }
            }



        } else {//没有选定分类    
          // if (this.data.r == 1) {//1是搜索
          //   if (this.data.minf == "") {
          //     data = {
          //       productName: this.data.keyword,
          //       // 'salePrice[0]': this.data.minf,
          //       'salePrice[1]': this.data.maxf
          //     }
          //   } else if (this.data.maxf == "") {
          //     data = {
          //       productName: this.data.keyword,
          //       'salePrice[0]': this.data.minf,
          //       // 'salePrice[1]': this.data.maxf
          //     }
          //   } else {
          //     data = {
          //       productName: this.data.keyword,
          //       'salePrice[0]': this.data.minf,
          //       'salePrice[1]': this.data.maxf
          //     }
          //   }

          // } else {//2是分类
            if (this.data.minf == "") {
              data = {
                categoryId: this.data.classId,
                // 'salePrice[0]': this.data.minf,
                'salePrice[1]': this.data.maxf
              }
            } else if (this.data.maxf == "") {
              data = {
                categoryId: this.data.classId,
                'salePrice[0]': this.data.minf,
                // 'salePrice[1]': this.data.maxf
              }
            } else {
              data = {
                categoryId: this.data.classId,
                'salePrice[0]': this.data.minf,
                'salePrice[1]': this.data.maxf
              }
           // }
          }

        }




      }


  
    
    
    
   
   
    
    
     
    wx.request({
      url: app.globalData.reqPath + 'shop/GoodsProducts',
      data: data,
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data }) {
        if (data.code == 200) {
          self.setData({list: data.data, showfilterBox: false })
        }
      }
    })
  },
  inputkeyword:function(e){//关键词输入
    this.setData({keyword:e.detail.value})
  },
  serch:function(){//搜索
      let r=this.data.r;
      let self=this;
      if(this.data.r==2){
        wx.request({
          url: app.globalData.reqPath + 'shop/GoodsProducts',
          header: {
            'authorization': app.globalData.authorization
          },
          data: {
            categoryId: this.data.classId,
            productName:this.data.keyword,
            limit: -1,
          },
          success: function ({ data, header }) {
            if (data.code == 200) {
              if (header.authorization && header.authorization != app.globalData.authorization) {
                app.globalData.authorization = header.authorization;
              }
              if (data.data.length == 0) {
                self.setData({ empty: true })
              }
              self.setData({ list: data.data })
            }
          }
        })
      }else{//搜索
        wx.request({
          url: app.globalData.reqPath + 'shop/GoodsProducts',
          header: {
            'authorization': app.globalData.authorization
          },
          data: {
            productName: this.data.keyword,
            limit: -1,
          },
          success: function ({ data, header }) {
            if (data.code == 200) {
              if (header.authorization && header.authorization != app.globalData.authorization) {
                app.globalData.authorization = header.authorization;
              }
              if (data.data.length == 0) {
                self.setData({ empty: true })
              }else{
                self.setData({ empty:false })
              }
              self.setData({ list: data.data })
            }
          }
        })
      }
  },
  changemin:function(e){//最低价输入
    this.setData({ minf:e.detail.value})
  },
  changemax:function(e){//最高价输入
    this.setData({ maxf: e.detail.value })
  }
})